﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmGiangVien : FormBase
    {
        public frmGiangVien()
        {
            InitializeComponent();
        }

        private void frmGiangVien_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Edit();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                using (NestedUnitOfWork nuow = xpCollection1.Session.BeginNestedUnitOfWork())
                {
                    frmGiangVien_Edit frm = new frmGiangVien_Edit();
                    var obj = new GiangVien(nuow);
                    frm.ItemEdit = obj;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        obj.Khoa = nuow.Query<Khoa>().FirstOrDefault(c => c.Oid == frm.KhoaID);
                        nuow.CommitChanges();
                        xpCollection1.Reload();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Edit();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (gridView1.GetFocusedRow() != null)
                {
                    if (CommonConfirm("Bạn muốn xóa dữ liệu này?", "XOA"))
                    {
                        using (NestedUnitOfWork nuow = xpCollection1.Session.BeginNestedUnitOfWork())
                        {
                            var obj = nuow.GetNestedObject(xpCollection1[gridView1.GetDataSourceRowIndex(gridView1.FocusedRowHandle)]) as GiangVien;
                            nuow.Delete(obj);
                            nuow.CommitChanges();
                        }
                        CommonMessage("Xóa thành công");
                        xpCollection1.Reload();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void Edit()
        {
            if (gridView1.GetFocusedRow() != null)
            {
                using (NestedUnitOfWork nuow = xpCollection1.Session.BeginNestedUnitOfWork())
                {
                    var obj = nuow.GetNestedObject(xpCollection1[gridView1.GetDataSourceRowIndex(gridView1.FocusedRowHandle)]) as GiangVien;
                    frmGiangVien_Edit frm = new frmGiangVien_Edit();
                    frm.ItemEdit = obj;
                    frm.KhoaID = obj.Khoa.Oid;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        obj.Khoa = nuow.Query<Khoa>().FirstOrDefault(c => c.Oid == frm.KhoaID);
                        nuow.CommitChanges();
                    }
                }
            }
        }
    }
}